package stackSample;

import java.lang.reflect.Method;
import java.util.Arrays;

public class State {

	private Class cls;
	private Method[] methods;
	private int[] enabledMethods;
	
	State(Class clss, int[] indexes) {		
		enabledMethods = indexes;
		Arrays.sort(enabledMethods);
		cls = clss;
		methods = cls.getDeclaredMethods();
		System.out.println();
	}

	public Class getStateClass() {
		return cls;
	}

	public Method[] getEnabledMethods(){

		Method[] result = new Method[enabledMethods.length];

		for (int i = 0; i < enabledMethods.length; i++) {
			result[i] = methods[enabledMethods[i]-1];
		}

		return result;
	}

	@Override
	public int hashCode() {
		return (cls.getName() + Arrays.toString(enabledMethods)).hashCode();
	}

	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		
		for (int i = 0; i < enabledMethods.length; i++) {
			sb.append("[");
			sb.append(methods[enabledMethods[i]-1].getName());
			sb.append("]");
		}

		return sb.toString();
	}

	@Override
	public boolean equals(Object obj){

		boolean result = false;

		if(obj instanceof State){
			State state = (State)obj;
			result = (this.hashCode() == state.hashCode());
		}

		return result;
	}
}